<?php

if (!session_is_registered('user')) exit();

$series = $_GET['series'];
$library = $_GET['library'];
$id = $_GET['id'];
$eid = $_GET['eid'];
$tvshow = $_GET['tvshow'];
$season = $_GET['season'];
$episode = $_GET['episode'];
$view = $_GET['view'];

$db2 = sqlite_open($gen['path'] . "\\library\\". $lib['file']);
$db = sqlite_open($gen['path'] . "\\library\\meeTVShows-Series.mlf");

if ((!$series) || ($view == "tree")) {
  if (!$view) $view = "classic";
  
  echo "<table width=\"100%\" style=\"margin-bottom: 20px;\">";
  echo "<tr><td colspan=\"2\" align=\"center\" style=\"font-size: 24; padding: 20px;\"><b>".$lib['name']."</b></td></tr>\n";
  echo "<tr>";
  echo "<td width=\"50%\" align=\"center\">";
  if ($view != "classic") echo "<a href=\"index.php?section=library&library=$library&view=classic\">";
  else echo "<b>";
  echo "Classic View";
  if ($view != "classic") echo "</a>";
  else echo "</b>";
  echo "</td>";
  echo "<td width=\"50%\"  align=\"center\">";
  if ($view != "tree") echo "<a href=\"index.php?section=library&library=$library&view=tree\">";
  else echo "<b>";
  echo "Tree View";
  if ($view != "tree") echo "</a>";
  else echo "</b>";
  echo "</td>";
  echo "</tr>";
  echo "</table>";

  if ($view == "classic") {

    echo "<table width=\"100%\">\n";
    $sql = sqlite_query($db, "SELECT item_id, item_name, item_image FROM items ORDER BY item_name ASC");
    if (sqlite_num_rows($sql) < 1) echo "No Series Found!";
    while ($data = sqlite_fetch_array($sql)) {
      $id = $data['item_id'];
      echo "<tr>\n";
      echo "<td width=\"100%\">\n";
      echo "<a href=\"index.php?section=library&library=$library&series=$id\">\n";
      if ($data['item_image']) echo "<img src=\"thumb.php?id=$id&library=$library\" border=\"0\">";
      else {
        echo "<div width=\"590\" align=\"center\" style=\"border: 1px solid black; padding: 30px; \">";
        echo "<b style=\"font-size: 20;\">".$data['item_name']."</b>";
        echo "</div>";
      }
      echo "</a>\n";
      echo "</td>\n";
      echo "</tr>\n";
    }
    echo "</table>\n";
  }
  
  elseif ($view == "tree") {
    echo "<div style=\"font-size: 20;\">";
    echo "<pre>";

    ## Get Episode Tags (New in 0.2)

    $sql_tags = sqlite_query($db2, "SELECT * FROM tags");
    while ($tags = sqlite_fetch_array($sql_tags)) {
      if ($tags['tag_name'] == "SeasonNumber")  $tag_season = $tags['tag_col'];
      if ($tags['tag_name'] == "SeriesID")      $tag_seriesid = $tags['tag_col'];
      if ($tags['tag_name'] == "EpisodeName")   $tag_episodename = $tags['tag_col'];
      if ($tags['tag_name'] == "EpisodeNumber") $tag_episodenumber = $tags['tag_col'];
    }

    $sql = sqlite_query($db, "SELECT item_ext_id, item_name, item_id FROM items ORDER BY item_name ASC");
    while ($serie = sqlite_fetch_array($sql)) {
      $id = $serie['item_id'];
      $tvid = $serie['item_ext_id'];
      echo "<a name=\"#$tvid\">";
      if ($tvshow == $tvid) {
        echo " <a href=\"index.php?section=library&library=$library&view=tree&tvshow=#$tvid\" style=\"text-decoration: none;\">-</a> ";
        echo $serie['item_name']."\n";
        echo "\t<a href=\"index.php?section=library&library=$library&series=$id\">Info</a>\n";
        $sql_seasons = sqlite_query($db2, "SELECT $tag_season FROM items WHERE $tag_seriesid='$tvid'
        GROUP BY $tag_season ORDER BY abs($tag_season)");
        while ($seasons = sqlite_fetch_array($sql_seasons)) {
          $sid = $seasons[$tag_season];
          echo "<a name=\"##$tvid$sid\">";
          if ($season == $sid) {
            echo "\t<a href=\"index.php?section=library&library=$library&view=tree&tvshow=$tvid&season=#$tvid$sid\" style=\"text-decoration: none;\">-</a> ";
            echo "Season $sid\n";
            $sql_episodes = sqlite_query($db2, "SELECT item_id, $tag_episodenumber, $tag_episodename, item_name FROM items WHERE $tag_season='$season' AND $tag_seriesid='$tvid' ORDER BY abs($tag_episodenumber)");
          
            while ($episodes = sqlite_fetch_array($sql_episodes)) {
              $e = $episodes['item_id'];
              echo "<a name=\"#$tvid$sid$e\">";
              if (strlen($episodes[$tag_episodenumber]) == 1) $number = "  ".$episodes[$tag_episodenumber];
              elseif (strlen($episodes[$tag_episodenumber]) == 2) $number = " ".$episodes[$tag_episodenumber];
              else $number = $episodes[$tag_episodenumber];
              if ($episode == $e) {
                echo "\t\t<a href=\"index.php?section=library&library=$library&view=tree&tvshow=$tvid&season=$sid&episode=#$tvid$sid$e\" style=\"text-decoration: none;\">-</a>";
                echo "$number ".wordwrap($episodes[$tag_episodename],30,"\n\t\t     ")."\n";
                echo "\t\t\t<a href=\"index.php?section=library&library=$library&series=$id&eid=$e\">Info</a>\n";

                if ($lib['streaming'] == "1") {
                  $string = str_replace($lib['path'], "", $filename[0]);
                  $link = $lib['umedia_folder']."\\".$string;
                  $host = $gen['host'].":".$gen['streamport'];
                  $open = "window.open('player.php?library=$library&id=$e&type=movie', 'Movie', 'feature=status=0,toolbar=0,location=0,directories=0,menubar=0,scrollbars=0,resizable=1');";
                  echo "\t\t\t<button onclick=\"$open\">Stream</button>\n";
                }
              }
              else {
                echo "\t\t<a href=\"index.php?section=library&library=$library&view=tree&tvshow=$tvid&season=$sid&episode=$e#$tvid$sid$e\" style=\"text-decoration: none;\">+</a>";
                echo "$number ".wordwrap($episodes[$tag_episodename],30,"\n\t\t     ")."\n";
              }
              echo "</a>";
            }
          }
          else {
            echo "\t<a href=\"index.php?section=library&library=$library&view=tree&tvshow=$tvid&season=$sid#$tvid$sid\" style=\"text-decoration: none;\">+</a> ";
            echo "Season $sid\n";
          }
          echo "</a>";
        }
      }
      else {
        echo " <a href=\"index.php?section=library&library=$library&view=tree&tvshow=$tvid#$tvid\" style=\"text-decoration: none;\">+</a> ";
        echo $serie['item_name']."\n";
      }
      echo "</a>";
    }
    echo "</pre>";
    echo "</div>";
  }
}


elseif (($series) && (!$id) && (!$eid)) {
  $sql = sqlite_query($db, "SELECT * FROM items WHERE item_id='$series'");
  $data = sqlite_fetch_array($sql);


  ## Get Series Tags (New in 0.2)

  $sql_tags = sqlite_query($db, "SELECT * FROM tags");

  while ($tags = sqlite_fetch_array($sql_tags)) {
    if ($tags['tag_name'] == "Airs")          $tag_airs = $tags['tag_col'];
    if ($tags['tag_name'] == "Genre")         $tag_genre = $tags['tag_col'];
    if ($tags['tag_name'] == "Runtime")       $tag_runtime = $tags['tag_col'];
    if ($tags['tag_name'] == "Actors")        $tag_actors = $tags['tag_col'];
    if ($tags['tag_name'] == "Status")        $tag_status = $tags['tag_col'];
    if ($tags['tag_name'] == "FirstAired")    $tag_firstaired = $tags['tag_col'];
    if ($tags['tag_name'] == "Overview")      $tag_overview = $tags['tag_col'];
    if ($tags['tag_name'] == "Network")       $tag_network = $tags['tag_col'];
  }


  $actors = $data[$tag_actors];
  if (strstr($actors, "|")) {
    $actors = substr($actors, 1, strlen($actors) - 2);
    $actors = str_replace("|", "<br>", $actors);
  }

  $id = $data['item_ext_id'];
  
  echo "<center>";
  if (!$data['item_image']) {
      echo "<div width=\"590\" align=\"center\" style=\"border: 1px solid black; padding: 30px; \">";
      echo "<b style=\"font-size: 20;\">".$data['item_name']."</b>";
      echo "</div>";
      echo "<br>";
  }
  else {
    echo "<img src=\"thumb.php?id=$series&library=$library\" border=\"0\"><br><br>";
  }
  echo "<a href=\"index.php?section=library&library=$library&series=$series&id=$id\">Seasons</a>";
  echo "</center>";
  echo "<table width=\"100%\" border=\"1\" style=\"margin-top: 20px;\">\n";
  echo "<tr>";
  echo "<td width=\"50%\"><b>Series Name:</b></td>";
  echo "<td width=\"50%\">".$data['item_name']."</td>";
  echo "</tr>";
  echo "<tr>";
  echo "<td width=\"50%\"><b>Airs:</b></td>";
  echo "<td width=\"50%\">".$data[$tag_airs]." on ".$data[$tag_network]."</td>";
  echo "</tr>";
  echo "<tr>";
  echo "<td width=\"50%\"><b>Genre:</b></td>";
  echo "<td width=\"50%\">".$data[$tag_genre]."</td>";
  echo "</tr>";
  echo "<tr>";
  echo "<td width=\"50%\"><b>Runtime:</b></td>";
  echo "<td width=\"50%\">".$data[$tag_runtime]."</td>";
  echo "</tr>";
  echo "<tr>";
  echo "<td width=\"50%\"><b>Status:</b></td>";
  echo "<td width=\"50%\">".$data[$tag_status]."</td>";
  echo "</tr>";
  echo "<tr>";
  echo "<td width=\"50%\" valign=\"top\"><b>Actors:</b></td>";
  echo "<td width=\"50%\">$actors</td>";
  echo "</tr>";
  echo "<tr>";
  echo "<td width=\"50%\"><b>First Aired:</b></td>";
  echo "<td width=\"50%\">".$data[$tag_firstaired]."</td>";
  echo "</tr>";
  echo "<tr>";
  echo "<td colspan=\"2\" style=\"padding-top: 20px;\">".str_replace("\n", "<br>", $data[$tag_overview])."</td>";
  echo "</tr>";

  echo "</table>";
}

elseif (($series) && ($id) && (!$eid)) {

  $si = sqlite_fetch_array(sqlite_query($db, "SELECT item_name, item_image FROM items WHERE item_id='$series'"));

  echo "<center>";
  if (!$si['item_image']) {
      echo "<div width=\"590\" align=\"center\" style=\"border: 1px solid black; padding: 30px; \">";
      echo "<b style=\"font-size: 20;\">".$si['item_name']."</b>";
      echo "</div>";
      echo "<br>";
  }
  else {
    echo "<img src=\"thumb.php?id=$series&library=$library\" border=\"0\"><br><br>";
  }
  echo "</center>";


  ## Get Episode Tags (New in 0.2)

  $sql_tags = sqlite_query($db2, "SELECT * FROM tags");

  while ($tags = sqlite_fetch_array($sql_tags)) {
    if ($tags['tag_name'] == "SeasonNumber")  $tag_season = $tags['tag_col'];
    if ($tags['tag_name'] == "SeriesID")      $tag_seriesid = $tags['tag_col'];
    if ($tags['tag_name'] == "EpisodeName")   $tag_episodename = $tags['tag_col'];
    if ($tags['tag_name'] == "EpisodeNumber") $tag_episodenumber = $tags['tag_col'];
  }


  $sql = sqlite_query($db2, "SELECT $tag_season FROM items WHERE $tag_seriesid='$id' GROUP BY $tag_season ORDER BY abs($tag_season)");
  while ($season = sqlite_fetch_array($sql)) {
    $s = $season[$tag_season];
    echo "<br><br><b style=\"font-size: 20;\">Season $s:</b><br><br>";
    $sql2 = sqlite_query($db2, "SELECT item_id, $tag_episodenumber, $tag_episodename, item_name FROM items WHERE $tag_season='$s' AND $tag_seriesid='$id' ORDER BY abs($tag_episodenumber)");
    while ($data = sqlite_fetch_array($sql2)) {
      $eid = $data['item_id'];
      echo "<a href=\"index.php?section=library&library=$library&series=$series&eid=$eid\">";
      echo "<b>".$data[$tag_episodenumber]."</b> ".$data[$tag_episodename]."</a><br>";
    }
  }

}
elseif ($eid) {

  ## Get Episode Tags (New in 0.2)

  $sql_tags = sqlite_query($db2, "SELECT * FROM tags");

  while ($tags = sqlite_fetch_array($sql_tags)) {
    if ($tags['tag_name'] == "SeasonNumber")  $tag_season = $tags['tag_col'];
    if ($tags['tag_name'] == "SeriesID")      $tag_seriesid = $tags['tag_col'];
    if ($tags['tag_name'] == "EpisodeName")   $tag_episodename = $tags['tag_col'];
    if ($tags['tag_name'] == "EpisodeNumber") $tag_episodenumber = $tags['tag_col'];
    if ($tags['tag_name'] == "GuestStars")    $tag_actors = $tags['tag_col'];
    if ($tags['tag_name'] == "FirstAired")    $tag_aired = $tags['tag_col'];
    if ($tags['tag_name'] == "Overview")      $tag_overview = $tags['tag_col'];
  }

  $si = sqlite_fetch_array(sqlite_query($db, "SELECT item_name, item_image FROM items WHERE item_id='$series'"));
  $sql = sqlite_query($db2, "SELECT * FROM items WHERE item_id='$eid'");
  $data = sqlite_fetch_array($sql);
  
  $actors = $data[$tag_actors];
  if (strstr($actors, "|")) {
    $actors = substr($actors, 1, strlen($actors) - 2);
    $actors = str_replace("|", "<br>", $actors);
  }

  echo "<center>";
  if (!$si['item_image']) {
      echo "<div width=\"590\" align=\"center\" style=\"border: 1px solid black; padding: 30px; \">";
      echo "<b style=\"font-size: 20;\">".$si['item_name']."</b>";
      echo "</div>";
      echo "<br>";
  }
  else {
    echo "<img src=\"thumb.php?id=$series&library=$library\" border=\"0\"><br><br>";
  }
  ## Streaming
  if ($lib['streaming'] == "1") {
    echo "<center>";
    $open = "window.open('player.php?library=$library&id=$eid&type=movie', 'Movie', 'feature=status=0,toolbar=0,location=0,directories=0,menubar=0,scrollbars=0,resizable=1');";
    echo "<button onclick=\"$open\">Stream</button>\n";
    echo "</center>";
  }
  echo "<table width=\"100%\" border=\"1\" style=\"margin-top: 20px;\">";
  
  echo "<tr>";
  echo "<td width=\"50%\"><b>Episode Name:</b></td>";
  echo "<td width=\"50%\">".$data[$tag_episodename]."</td>";
  echo "</tr>";

  echo "<tr>";
  echo "<td width=\"50%\"><b>Season:</b></td>";
  echo "<td width=\"50%\">".$data[$tag_season]."</td>";
  echo "</tr>";
  
  echo "<tr>";
  echo "<td width=\"50%\"><b>Episode:</b></td>";
  echo "<td width=\"50%\">".$data[$tag_episodenumber]."</td>";
  echo "</tr>";
  
  echo "<tr>";
  echo "<td width=\"50%\"><b>Aired:</b></td>";
  echo "<td width=\"50%\">".$data[$tag_aired]."</td>";
  echo "</tr>";

  echo "<tr>";
  echo "<td width=\"50%\" valign=\"top\"><b>Guest Stars:</b></td>";
  echo "<td width=\"50%\">$actors</td>";
  echo "</tr>";
  
  echo "<tr>";
  echo "<td width=\"50%\" colspan=\"2\">".$data[$tag_overview]."</td>";
  echo "</tr>";
  
  echo "</table>";
}
?>
